package org.belkodevelop.controller.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.belkodevelop.controller.helper.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

public class UserInterceptor extends HandlerInterceptorAdapter {	

    @Autowired
    private AuthUtils authUtils;

    public static final String USER_OBJECT_NAME = "user";
    
    @Override
    public void postHandle(HttpServletRequest request,
                    HttpServletResponse response, Object handler,
                    ModelAndView modelAndView) throws Exception {
        
        if (modelAndView != null) {
            if (modelAndView.getModel().get(USER_OBJECT_NAME) == null) {
                modelAndView.addObject(USER_OBJECT_NAME, authUtils.getCurrentSessionUser());
            }
        }
        super.postHandle(request, response, handler, modelAndView);
    }
	
}
